package connect4swt.gui;

import game.Player;
import game.PlayerMove;

import java.util.ArrayList;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Canvas;
import connect4swt.gui.Piece;

/**
 * @author  Joel Cordeiro
 */
public abstract class Board {
	
	public Board(Canvas parentCanvas, Player me, Player opponent) {
		this.width = parentCanvas.getSize().x;
		this.height = Math.round(parentCanvas.getSize().y * 0.80f);
		this.yAxisOffset = parentCanvas.getSize().y - this.height;
		this.pieces = new ArrayList<Piece>();				
		
		this.me = me;
		this.opponent = opponent;
		
		this.parentCanvas = parentCanvas;
	}
	
	protected abstract void computeResize();
	
	/**
	 * Initializes the board
	 *
	 */
	protected abstract void createPieces(); 
	
	//If we have an agreement about this way of displaying game information, we'll have to
	//play with text size and its position, otherwise we'll display that information
	//in a label or textbox!
	protected abstract void drawGameInformationMessage(String message);
	protected abstract void drawPlayersInformation();		
	protected abstract void drawGameBoard(); 
	protected abstract void fillPiece(PlayerMove move);		
	public abstract void playPiece(Point oldPosition, Point newPosition);
	protected abstract Piece getPieceFromMousePosition(Point position);	
	protected abstract Piece getPieceFromBoardPosition(Point position);
	protected abstract void drawPiece(Piece piece);
	
	protected void paint() {
		computeResize();			
		drawGameBoard();
		drawPlayersInformation();
	}					
	
	protected void definePlayers()
	{
		if(playerOneTurn){
			this.player1 = me.getLogin();
			this.player2 = opponent.getLogin();
					
			this.ranking1 = me.getRanking();
			this.ranking2 = opponent.getRanking();
			
			this.direction = 1;
		}
		else 
		{
			this.player2 = me.getLogin();
			this.player1 = opponent.getLogin();
					
			this.ranking2 = me.getRanking();
			this.ranking1 = opponent.getRanking();
			
			this.direction = -1;
		}
				
	}
	
	/**
	 * @return
	 * @uml.property  name="direction"
	 */
	public int getDirection()
	{
		return this.direction;
	}
	
	/**
	 * @return
	 * @uml.property  name="playerOneTurn"
	 */
	public boolean isPlayerOneTurn() {
		return playerOneTurn;
	}

	/**
	 * @param playerOneTurn
	 * @uml.property  name="playerOneTurn"
	 */
	public void setPlayerOneTurn(boolean playerOneTurn) {
		this.playerOneTurn = playerOneTurn;
	}
	
	/**
	 * @param matchId
	 * @uml.property  name="matchId"
	 */
	public void setMatchId(int matchId){
		this.matchId = matchId;
	}
	
	/**
	 * @return
	 * @uml.property  name="isMatchOver"
	 */
	public boolean isMatchOver(){
		return this.isMatchOver;
	}
	
	/**
	 * @param isMatchOver
	 * @uml.property  name="isMatchOver"
	 */
	public void setMatchOver(boolean isMatchOver){
		this.isMatchOver = isMatchOver;
	}
	
	/**
	 * @return
	 * @uml.property  name="parentCanvas"
	 */
	public Canvas getParentCanvas(){
		return this.parentCanvas;
	}
	
	protected int width;
	protected int height;
	protected int yAxisOffset;
	protected ArrayList<Piece> pieces;
	protected Canvas parentCanvas;
	protected GC gc;
	protected boolean playerOneTurn = false;
	protected int matchId;
	protected boolean isMatchOver = false;
		
	protected String player1;
	protected String player2;
	protected int ranking1;
	protected int ranking2;	

	protected Player me;
	protected Player opponent;
	private int direction = 0;
}
